﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Business.DbConnect;

namespace Business
{
    public class TicketBusiness
    {
        /// <summary>
        /// Select All Ticket
        /// </summary>
        /// <returns></returns>
        public static List<Ticket> Select()
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                return entities.Tickets.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Select Ticket by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Ticket Select(Int64 id)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                return entities.Tickets.FirstOrDefault(ob => ob.TicketId == id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Select ticket type by plate
        /// </summary>
        /// <param name="plate"></param>
        /// <returns></returns>
        public static TicketType SelectTypeByPlate(String plate)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var query = from tk in entities.Tickets
                            where tk.MotorbikePlate == plate
                            join tkType in entities.TicketTypes
                            on tk.TypeId equals tkType.Id
                            select tkType;

                return query.First();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Search by custom search query
        /// </summary>
        /// <param name="sContent"></param>
        /// <returns></returns>
        public static List<Ticket> Search(String sContent)
        {
            // Make input valid
            sContent = (sContent + String.Empty).ToLower();

            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var query = from tk in entities.Tickets
                            join cus in entities.Customers
                            on tk.CustomerId equals cus.Id
                            where (tk.MotorbikePlate + String.Empty).ToLower().Contains(sContent)
                                || (cus.FullName + String.Empty).ToLower().Contains(sContent)
                            select tk;

                return query.Take(1000).ToList();
            }
            catch
            {
                return new List<Ticket>();
            }
        }

        /// <summary>
        /// Insert Ticket
        /// </summary>
        /// <param name="tk"></param>
        /// <returns></returns>
        public static bool Insert(Ticket tk)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                entities.Tickets.Add(tk);
                entities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Ticket
        /// </summary>
        /// <param name="tk"></param>
        /// <returns></returns>
        public static bool Update(Ticket tk)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var ticket = entities.Tickets.Find(tk.TicketId);
                entities.Entry(ticket).CurrentValues.SetValues(tk);
                entities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Ticket
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(Int64 id)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var ticket = entities.Tickets.First(ob => ob.TicketId == id);
                entities.Entry(ticket).State = EntityState.Deleted;
                entities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}